import { createStore } from "vuex";
import axios from "axios";

const store = createStore({
  state() {
    return {
      sellerIofo: {},
      goods: [],
      show: false,
      carList: [],
      price: 0,
    };
  },
  getters: {
    getPrice(state) {
    
    },
  },
  mutations: {
    setSeller(state, payload) {
      state.sellerIofo = payload.data;
    },
    setGoods(state, payload) {
      state.goods = payload.data;
    },
    showCar(state) {
      state.show = !state.show;
    },
    setCarList(state, payload) {
      const hasItem = state.carList.findIndex((item) => {
        return item.name == payload.item.name;
      });

      if (hasItem == "-1") {
        state.carList.push(payload.item);
      }
    },
    setCar(state){
      state.carList=[];
      state.goods.forEach( function(item,index){
        item.foods.forEach( function (item1,index){
        item1.count=0

        })
      })
    }
  },
  actions: {
    async getGoods({ commit }) {
      const {
        data: { data },
      } = await axios.get("/api/goods");
      commit("setGoods", { data });
    },
    async getSeller({ commit }) {
      const {
        data: { data },
      } = await axios.get("/api/seller");

      commit("setSeller", {
        data,
      });
    },
  },
});

export default store;





